/**
 * 项目名称: Topic_703.cpp
 * 项目路径: study/DataStructure/LeetCode/树/简单
 * 编辑时间: 2024年08月22日08时12分10秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class KthLargest {
public:
    int ans = 0;
    vector<int>vk;
    TreeNode * T = nullptr;
    KthLargest(int k, vector<int>& nums) {
        ans= k;
        for(auto x:nums)createTree(T,x);
    }

    void createTree(TreeNode *&T,int k){
        if(!T){
            T = new TreeNode;
            T->val = k;
            T->left = T->right = nullptr;
        }
        else if(k <= T->val)return createTree(T->left,k);
        else return createTree(T->right,k);
    }

    void pre(TreeNode *root){
        if(root){
            pre(root->left);
            vk.push_back(root->val);
            pre(root->right);
        }
    }

    int add(int val) {
        vk.clear();
        createTree(T,val);
        pre(T);
        for(auto x:vk)cout<<x<<" ";
        cout<<endl;
        return vk[vk.size()-ans];
    }
};
